#!/bin/sh
# 自动创建 bucket 并设置 public/private 策略

MC_ALIAS=myminio
MINIO_URL=http://minio:9000
MINIO_USER=${MINIO_ROOT_USER:-admin}
MINIO_PASS=${MINIO_ROOT_PASSWORD:-admin123}
BUCKET_NAME=data

# 等待 MinIO 完全启动
echo "⏳ 等待 MinIO 启动..."
until mc alias set $MC_ALIAS $MINIO_URL $MINIO_USER $MINIO_PASS 2>/dev/null; do
  sleep 2
done
echo "✅ MinIO 已启动"

# 创建 bucket
mc mb $MC_ALIAS/$BUCKET_NAME --ignore-existing

# 创建 public/private 目录（空文件占位）
touch /tmp/.keep
mc cp /tmp/.keep $MC_ALIAS/$BUCKET_NAME/public/.keep
mc cp /tmp/.keep $MC_ALIAS/$BUCKET_NAME/private/.keep

# 创建 policy
mc admin policy create $MC_ALIAS bucket-policy /policy/bucket-policy.json || true

# 设置 public 匿名下载
mc anonymous set download $MC_ALIAS/$BUCKET_NAME/public

echo "✅ Bucket 和策略已配置完成"